<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>管理员列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="user-search">
      <el-form-item label="搜索：">
        <el-input size="small" v-model="formInline.managerAccountNumber" placeholder="输入管理员名称"></el-input>
      </el-form-item>
      <el-form-item label="">
        <el-input size="small" v-model="formInline.managerName" placeholder="输入管理员账号"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        <el-button size="small" type="primary" icon="el-icon-plus" @click="handleEdit()">添加</el-button>
      </el-form-item>
    </el-form>
    <!--批量删除按钮-->
    <el-button size="small" type="warning" icon="el-icon-delete-solid" @click="dataDel(multipleSelection)" style="margin-bottom: 10px">全部删除</el-button>
    <!--列表-->

<!--    {-->
<!--    managerAccountNumber:'manager001',-->
<!--    managerName:'池松松',-->
<!--    roleName:'超级管理员',-->
<!--    managerGender:'1',-->
<!--    insertTime:'2022-5-20 15:21',-->
<!--    modifiedTime:'2022-5-20 15:21',-->
<!--    }-->

    <el-table size="small" :data="listData" highlight-current-row v-loading="loading" border element-loading-text="拼命加载中"   @selection-change="handleSelectionChange" style="width: 100%;">
      <el-table-column align="center" type="selection" width="60">
      </el-table-column>
      <el-table-column sortable prop="managerAccountNumber" label="管理员账号" width="200">
      </el-table-column>
      <el-table-column sortable prop="managerName" label="管理员名称" width="150">
      </el-table-column>
      <el-table-column sortable prop="administratorsRole.roleName" label="角色名称" width="150">
      </el-table-column>
      <el-table-column sortable prop="managerGender" label="性别" width="100" :formatter="formatRole">
      </el-table-column>
      <el-table-column sortable prop="insertTime" label="添加时间" width="220">
      </el-table-column>
      <el-table-column sortable prop="modifiedTime" label="修改时间" width="220">
      </el-table-column>
      <el-table-column align="center" label="操作" min-width="300">
        <template slot-scope="scope">
          <!--<el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>-->
          <el-button size="mini" @click="dataRestManagerPassword(scope.row)">重置密码</el-button>
          <!--/*scope.$index,*/-->
          <el-button size="mini" type="danger" @click="dataDel(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination v-bind:childMsg="pageparm" @callFather="callFather"></Pagination>
    <!-- 编辑界面 -->
    <el-dialog :title="title" :visible.sync="editFormVisible" width="30%" @click="closeDialog">
      <el-form label-width="120px" :model="editForm" :rules="rules" ref="editForm">
        <el-form-item label="管理员账号" prop="managerAccountNumber">
          <el-input size="small" v-model="editForm.managerAccountNumber" auto-complete="off" placeholder="请输入部门名称"></el-input>
        </el-form-item>
        <el-form-item label="管理员名称" prop="managerName">
          <el-input size="small" v-model="editForm.managerName" auto-complete="off" placeholder="请输入部门代码"></el-input>
        </el-form-item>
        <el-form-item label="管理员角色" prop="roleNumber">
          <el-select size="small" v-model="editForm.roleNumber" auto-complete="off" placeholder="请选择">
            <el-option v-for="type in roles" :label="type.roleName"  :value="type.roleNumber" ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="管理员性别" prop="managerGender">
<!--          <el-radio v-model="editForm.managerGender" value="1" label="男">男</el-radio>-->
<!--          <el-radio v-model="editForm.managerGender" value="0" label="女">女</el-radio>-->
          <el-radio-group v-model="editForm.managerGender">
            <el-radio label="1">男</el-radio>
            <el-radio label="0">女</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog">取消</el-button>
        <el-button size="small" type="primary" :loading="loading" class="title" @click="submitForm('editForm')">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

  import { managerList,managerInsert,managerDel,resetManagerPassword,getGenerateManagerAccountNumber,roleList} from '../../api/personnelMG.js'
  import Pagination from '../../components/Pagination'
  export default {
    name: 'Managers',
    data() {
      return {
        nshow: true, //switch开启
        fshow: false, //switch关闭
        loading: false, //是显示加载
        editFormVisible: false, //控制编辑页面显示与隐藏
        title: '添加',
        editForm: {
          managerAccountNumber: '',
          managerName: '',
          roleNumber: '',
          managerGender: '',
          token: localStorage.getItem('logintoken')
        },
        // rules表单验证
        rules: {
          managerAccountNumber: [{ required: true, message: '请输入管理员名称', trigger: 'blur' }],
          managerName: [{ required: true, message: '请输入管理员名称', trigger: 'blur' }],
          roleNumber:[{required: true, message: '请选择',trigger: 'blur'}],
          managerGender:[{required: true, message: '请选择',trigger: 'blur'}]
        },
        formInline: {
          page: 1,
          limit: 10,
          managerAccountNumber: '',
          managerName: '',
          token: localStorage.getItem('logintoken')
        },
        userparm: [], //搜索权限
        listData: [], //用户数据
        currentRow:null,
        multipleSelection:[],
        roles:[
          {
            roleNumber:'001',
            roleName:"超级管理员"
          }
        ],
        // 分页参数
        pageparm: {
          currentPage: 0,
          pageSize: 10,
          total: 0
        }
      }
    },
    // 注册组件
    components: {
      Pagination
    },
    /**
     * 数据发生改变
     */

    /**
     * 创建完毕
     */
    created() {
      this.getData(this.formInline)
    },

    /**
     * 里面的方法只有被调用才会执行
     */
    methods: {
      //判断男女tt
      formatRole:function(row, column){
        return row.managerGender == '1' ? "男" : row.managerGender == '0' ? "女" : "";
      },
      //多选框
      handleSelectionChange(val){
        //alert(JSON.stringify(val))
        this.multipleSelection=val
      },
      //删除
      dataDel(row){
        let rows=[];
        rows.push(row)
        //alert(JSON.stringify(rows))
        this.$confirm('确定要删除吗?', '信息', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(()=>{
          let managerAccountNumbers=[]
          rows.forEach(manager=>{
            managerAccountNumbers.push(manager.managerAccountNumber)
          })
          let param={"dataList":managerAccountNumbers}
          managerDel(param).then(res=>{
            if(res.code==200){
              this.$message({
                type: 'success',
                message: '删除成功'
              })
            }else{
              let managerAccountNumbers=res.data
              this.$message({
                type: 'error',
                message: '删除失败，未删除的:'+JSON.stringify(managerAccountNumbers)
              })
            }
            this.getData(this.formInline)
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })

      },
      /*获取角色列表*/
      getRoleData(){
        roleList().then(res=>{
          //alert(JSON.stringify(res))
          if(res.code==200){
            this.roles=res.data
          }else{
            this.$message({
              type:'error',
              message:'请求角色列表失败'
            })
          }
        })
      },
      //重置密码
      dataRestManagerPassword(row){
        let param={"managerAccountNumber":row.managerAccountNumber}
        resetManagerPassword(param).then((res)=>{
          if(res.code==200){
            this.$message({
              type: 'success',
              message: '重置成功,密码:'+res.data
            })
          }else{
            this.$message({
              type: 'error',
              message: '重置失败'
            })
          }
        })
      },

      // 获取管理员列表
      getData(params) {
        this.loading = true

        managerList(params).then(res => {
          //alert(JSON.stringify(res))
          if(res.code=="200") {
            this.loading = false
            //this.listData = res.data
            this.listData = res.data.records
            this.pageparm.currentPage = res.data.current
            this.pageparm.pageSize = res.data.size
            this.pageparm.total = res.data.total
            this.formInline.page = res.data.current
            this.formInline.limit = res.data.size
          }else if(res.code=="403.1"){
            this.$message({
              type:'error',
              message:'超时过期'
            })
          }

          //       managerAccountNumber:'manager001',
          //       managerName:'池松松',
          //       roleName:'超级管理员',
          //       managerGender:'1',
          //       insertTime:'2022-5-20 15:21',
          //       modifiedTime:'2022-5-20 15:21',
        })
      },
      // 分页插件事件
      callFather(parm) {
        this.formInline.page = parm.currentPage
        this.formInline.limit = parm.pageSize
        this.getData(this.formInline)
      },
      // 搜索事件
      search() {
        this.getData(this.formInline)
      },
      //显示编辑界面
      handleEdit: function(index,row) {

        //获取自动生成的管理员编号
        getGenerateManagerAccountNumber().then(res=>{
          if(res.code==200) {
            this.editForm.managerAccountNumber = res.data;
          }else{
            this.$message({
              type:'error',
              message:'请求失败'
            })
          }

        })


        this.editFormVisible = true
        this.getRoleData();
        if (row != undefined && row != 'undefined') {
          this.title = '修改'
          this.editForm.managerAccountNumber= ''
          this.editForm.managerName= ''
          this.editForm.roleNumber= ''
          this.editForm.managerGender= ''

        } else {
          this.title = '添加'
          this.editForm.managerAccountNumber= ''
          this.editForm.managerName= ''
          this.editForm.moleNumber= ''
          this.editForm.managerGender= ''
        }

      },
      // 编辑、增加页面保存方法
      submitForm(editData) {
        this.$refs[editData].validate(valid => {
          if (valid) {
            managerInsert(this.editForm).then(res=>{
              if(res.code==200){
                this.$message({
                  type:'success',
                  message:'成功'
                })
                this.closeDialog();
                this.getData(this.formInline)
              }else{
                this.$message({
                  type:'error',
                  message:'失败'
                })
                this.closeDialog();
              }
            }).catch(res=>{
              this.$message({
                type:'error',
                message:'错误'
              })
            })


          } else {
            return false
          }
        })
      },
      // 关闭编辑、增加弹出框
      closeDialog() {
        this.editFormVisible = false
      }
    }
  }
</script>

<style scoped>
  .user-search {
    margin-top: 20px;
  }
  .userRole {
    width: 100%;
  }
</style>
